<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of OrderController
 *
 * @author simon.zwicker
 */
class OrderController extends Zend_Controller_Action
{
    public function init()
    {
         if ($_SESSION['logged'] != TRUE) {
            header('location: /index/login');
        }
        parent::init();
    }
    
    protected function getOrderForm()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $form = new Zend_Form;
        $form->setAction('/order/checkorder')->setMethod('post');
        
        $caterer = new Zend_Form_Element_Text('caterer', array('label' => 'WO? (ID)'));
        $caterer->setAttrib('size', '10');
        
        $eating = new Zend_Form_Element_Text('eating',array('label' => 'WAS?'));
        $eating->setAttrib('size', '38');
        
        $form->addElement($caterer)
             ->addElement($eating)
             ->addElement('submit','order',array('label' => 'Bestellen'));
        
        return $form;
    }
    
    public function orderAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        if ($_SESSION['bestellung'] != TRUE) {
            $this->view->form = $this->getOrderForm();
        } else {
            $this->view->form = 'Du hast bereits eine Bestellung abgeschickt, die nächste Bestellung kann erst morgen wieder gemacht werden!';
        }
        
        echo '<div class="t2">
                    <h3>Caterer & Speisekarten:</h3>
                        <table>';
        $data = $db->fetchAll('SELECT c.caterer_id,a.name,a.address,a.plz,a.city,a.phone,c.card FROM adresse a,caterer c WHERE a.address_id = c.address_id');
        for ($i = 0; $i < count($data); $i++) {
            echo '<tr>
                    <td>
                        Caterer-ID: <b>'. $data[$i]['caterer_id'] .'</b><br />
                        '. $data[$i]['name'] .'<br />
                        '. $data[$i]['plz'] .' '. $data[$i]['city'] .'<br />
                    </td>
                    <td style="text-align: right; width: 200px; height: 80px">
                        <a target="_blank" href="'. $data[$i]['card'] .'">Zur Speisekarte</a>
                    </td>
                </tr>';
        }
        echo '</table></div>';
    
    }
    
    public function checkorderAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $order_param = $this->getRequest()->getParams();
        
        if (!empty($order_param['eating'])) {
            $data = $db->fetchAll('SELECT b.order_id,b.firma_id FROM bestellung b,firma f,adresse a WHERE a.name = "'. $_SESSION['firma'] .'" AND a.address_id = f.address_id AND f.firma_id = b.firma_id');
            if ($data) {
                $order_id = $data[0]['order_id'];
                $firma_id = $data[0]['firma_id'];
                
                date_default_timezone_set('Europe/Paris');
                $userdate = md5(date('d.m.Y').':'.$_SESSION['user_id']);
                $db->insert('essen', array(
                    'user_date' => $userdate,
                    'order_id' => $order_id,
                    'caterer_id' => $order_param['caterer'],
                    'essen' => $order_param['eating']
                ));
                $_SESSION['bestellung'] = TRUE;
                $_SESSION['bestellungE'] = $order_param['eating'];
                $_SESSION['bestellungC'] = $order_param['caterer'];
                
            }
        } else {
            $this->view->ordermsg = 'Bitte trage ein was du bei dem Caterer Essen möchtest';
        }
    }
}


